package com.yorun.lib_wx_pay;

import android.content.Context;
import android.util.Log;
import android.util.Pair;
import android.widget.Toast;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yorun on 2016/12/21.
 */

public class YWXPays {

    public static final String TAG = "Y->WXPAY";
    public static final Config config = new Config();
    private static IWXAPI wxapi;

    public static Config config() {
        return config;
    }
    public static void register(Context context) {
        final IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);
        // 将该app注册到微信
        Log.d(TAG,"注册,"+config.appid+","+msgApi.registerApp(config.appid)+"");
        wxapi = WXAPIFactory.createWXAPI(context, config.appid);
    }
    public static String genGetPrepayIdPostString(PostEntity postEntity){

        postEntity.appid = config.appid;
        postEntity.mch_id = config.mchId;
        postEntity.nonce_str = Utils.genNonceStr();
        Log.d(TAG,","+postEntity);
        postEntity.sign= Utils.genPackageSign(new Pair[]{
                new Pair("appid", postEntity.getAppid()),
                new Pair("attach", postEntity.getAttach()),
                new Pair("body", postEntity.getBody()),
                new Pair("mch_id", postEntity.getMch_id()),
                new Pair("nonce_str", postEntity.getNonce_str()),
                new Pair("notify_url", postEntity.getNotify_url()),
                new Pair("out_trade_no", postEntity.getOut_trade_no()),
                new Pair("spbill_create_ip", postEntity.getSpbill_create_ip()),
                new Pair("total_fee", postEntity.getTotal_fee()),
                new Pair("trade_type", postEntity.getTrade_type())
        });
        return Utils.objectToXml(postEntity, "xml");
    }

    public static boolean pay(String prepayId) {

        PayReq req = new PayReq();
        //req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
        req.appId			=config.appid;
        req.partnerId		=config.mchId;
        req.prepayId		=prepayId;
        req.nonceStr		=Utils.genNonceStr();
        req.timeStamp		=(System.currentTimeMillis()+"").substring(0,10);
        req.packageValue	= "Sign=WXPay";

        req.sign = Utils.genPackageSign(new Pair[]{
                new Pair("appid", req.appId),
                new Pair("noncestr", req.nonceStr),
                new Pair("package", req.packageValue),
                new Pair("partnerid", req.partnerId),
                new Pair("prepayid", req.prepayId),
                new Pair("timestamp", req.timeStamp)
        });
//							req.extData			= "app data"; // optional
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        return  wxapi.sendReq(req);
    }


    public static class Config {
         String appid;
         String mchId;
         String mchKey;
        public Config appId(String appid) {
            this.appid = appid;
            return this;
        }
        public Config mchId(String mchId) {
            this.mchId = mchId;
            return this;
        }
        public Config mchKey(String mchKey) {
            this.mchKey = mchKey;
            return this;
        }

        public String getAppid() {
            return appid;
        }

        public String getMchId() {
            return mchId;
        }

        public String getMchKey() {
            return mchKey;
        }
    }

}
